package com.first.nell.loan

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.first.nell.base.BaseViewModel
import com.first.nell.data.ReqBase
import com.first.nell.network.ApiUserReposity
import com.first.nell.utils.AppConfigUtil
import com.first.nell.utils.UsageCountryUtil

/** 贷款viewModel */
class LoanViewModel : BaseViewModel() {

    /** 查询间隔多少天后能重新申请 */
    val onFirstNellFetchFirstNellNextFirstNellApplyFirstNellDaysFirstNellLiveData =
        MutableLiveData<String>()

    /** 查询贷款额度信息 */
    private val mFirstNellFetchFirstNellLoanFirstNellConfirmFirstNellDetailFirstNellLiveData =
        MutableLiveData<ReqBase>()

    /** 查询放款中信息 */
    private val mFirstNellFetchFirstNellLoaningFirstNellDetailFirstNellLiveData =
        MutableLiveData<ReqBase>()

    /** 创建贷款申请 */
    private val mFirstNellCreateFirstNellLoanFirstNellLiveData = MutableLiveData<ReqBase>()

    /** 查询app评价流程 */
    private val mCheckFirstNellAppFirstNellReviewFirstNellLiveData = MutableLiveData<ReqBase>()

    /** 查询贷款额度信息 */
    val onFirstNellFetchFirstNellLoanFirstNellConfirmFirstNellDetailFirstNellLiveData =
        Transformations.switchMap(
            mFirstNellFetchFirstNellLoanFirstNellConfirmFirstNellDetailFirstNellLiveData
        ) { query ->
            ApiUserReposity.fetchFirstNellLoaningFirstNellDetail(this, query)
        }

    /** 查询放款中信息 */
    val onFirstNellFetchFirstNellLoaningFirstNellDetailFirstNellLiveData =
        Transformations.switchMap(mFirstNellFetchFirstNellLoaningFirstNellDetailFirstNellLiveData) { query ->
            ApiUserReposity.fetchFirstNellLoaningFirstNellDetail(this, query)
        }

    /** 创建贷款申请 */
    val onFirstNellCreateFirstNellLoanFirstNellLiveData =
        Transformations.switchMap(mFirstNellCreateFirstNellLoanFirstNellLiveData) { query ->
            ApiUserReposity.createFirstNellLoan(this, query)
        }

    /** 查询app评介流程 */
    val onCheckFirstNellAppFirstNellReviewFirstNellLiveData =
        Transformations.switchMap(mCheckFirstNellAppFirstNellReviewFirstNellLiveData) { query ->
            ApiUserReposity.checkFirstNellAppFirstNellReview(this, query)
        }

    /** 查询间隔多少天后能重新申请 */
    fun fetchFirstNellNextFirstNellApplyFirstNellDays() {
        val nextApplyDays = AppConfigUtil.getCashArrwoReapplyFirstNellDays()
        var nextApplyDaysString = "please apply again ${nextApplyDays} days later."
        if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva() or UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
            nextApplyDaysString = "veuillez réappliquer ${nextApplyDays} jours plus tard."

        } else if (UsageCountryUtil.isFirstNellCountryFirstNellVietnam()) {
            nextApplyDaysString = "vui lòng đăng ký lại sau ${nextApplyDays} ngày."
        }
        onFirstNellFetchFirstNellNextFirstNellApplyFirstNellDaysFirstNellLiveData.value =
            nextApplyDaysString
    }

    /** 查询贷款额度信息 */
    fun fetchFirstNellLoanFirstNellConfirmFirstNellDetail(reqBase: ReqBase) {
        mFirstNellFetchFirstNellLoanFirstNellConfirmFirstNellDetailFirstNellLiveData.value = reqBase
    }

    /** 查询放款中信息 */
    fun fetchFirstNellLoaningFirstNellDetail(reqBase: ReqBase) {
        mFirstNellFetchFirstNellLoaningFirstNellDetailFirstNellLiveData.value = reqBase
    }

    /** 创建贷款申请 */
    fun createFirstNellLoan(reqBase: ReqBase) {
        mFirstNellCreateFirstNellLoanFirstNellLiveData.value = reqBase
    }

    /** 查询app评介流程 */
    fun checkFirstNellAppFirstNellReview(reqBase: ReqBase) {
        mCheckFirstNellAppFirstNellReviewFirstNellLiveData.value = reqBase
    }
}